package com.zhiyou.aifeng.smart_machine

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.InputFilter
import com.jeremyliao.liveeventbus.utils.observe
import com.zhiyou.aifeng.common.base.oneClick
import com.zhiyou.aifeng.common.helper.*
import com.zhiyou.aifeng.common_ui.base.BaseActivity
import com.zhiyou.aifeng.common_ui.base.addItems
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.model.CommonData
import com.zhiyou.aifeng.common_ui.utils.showHintDialog
import com.zhiyou.aifeng.common_ui.utils.showListDialog
import com.zhiyou.aifeng.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_wallet_cash.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class WalletCashActivity : BaseActivity() {

    private var mCardNum = ""
    private val mCardList = ArrayList<CommonData>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wallet_cash)
        initTitle(title = "申请提现", name = "提现记录")

        getData()
    }

    override fun getViewModel() = get<WalletViewModel>()

    @SuppressLint("SetTextI18n")
    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        cash_input.filters = arrayOf<InputFilter>(DecimalNumberFilter())
        cash_hint.text = "当前可提现金额：${getString("balance")}元（满100元可提现）"

        observe("cardList") { getData() }

        tvRight?.oneClick { startActivity<WalletCashListActivity>() }
        cash_bank.oneClick {
            if (mCardList.isEmpty()) {
                showHintDialog(
                    title = "添加银行卡",
                    hint = "您还未添加银行卡，是否确定去添加？"
                ) {
                    startActivity<WalletAddActivity>()
                }
            } else {
                showListDialog(
                    title = "选择银行卡",
                    items = mCardList.mapTo(ArrayList()) {
                        "${it.bankname}（${it.cardnumber.substring(it.cardnumber.length - 4)}）"
                    }
                ) { index, _ ->
                    mCardNum = mCardList[index].cardnumber
                    cash_bank.setLeftString(
                        "${mCardList[index].bankname}（${mCardNum.substring(
                            mCardNum.length - 4
                        )}）"
                    )
                }
            }
        }
        bt_apply.oneClick {
            when {
                mCardList.isEmpty() -> {
                    toast("请添加提现银行卡")
                    return@oneClick
                }
                cash_input.text.isEmpty() -> {
                    cash_input.requestFocus()
                    toast("请输入提现金额")
                    return@oneClick
                }
                cash_input.text.toTextDouble() > getString("balance").toNotDouble() -> {
                    toast("可提现金额不足")
                    return@oneClick
                }
                cash_input.text.toTextDouble() < 100 -> {
                    toast("可提现金额不少于100元")
                    return@oneClick
                }
            }

            getViewModel().request(
                requestUrl = BaseUrl.walletDraw,
                params = *arrayOf(
                    "userId" to getString("userId"),
                    "cardnumber" to mCardNum,
                    "account" to cash_input.textString()
                ),
                onResult = {
                    toast("申请提现提交成功，请耐心等待！")
                    finishView()
                }
            )
        }
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().requestModel(
            isLoading = true,
            requestUrl = BaseUrl.walletCard,
            params = *arrayOf("userId" to getString("userId")),
            onSuccess = {
                mCardList.clear()
                mCardList.addItems(it)

                if (mCardList.isNotEmpty()) {
                    mCardNum = mCardList[0].cardnumber
                    cash_bank.setLeftString("${mCardList[0].bankname}（${mCardNum.substring(mCardNum.length - 4)}）")
                } else {
                    cash_bank.setLeftString("点击添加银行卡")
                }
            }
        )
    }

}